package cn.tedu._07vehicle.controller;

import cn.tedu._07vehicle.base.response.JsonResult;
import cn.tedu._07vehicle.base.response.StatusCode;
import cn.tedu._07vehicle.mapper.UserMapper;
import cn.tedu._07vehicle.pojo.dto.UserLoginParam;
import cn.tedu._07vehicle.pojo.dto.UserRegParam;
import cn.tedu._07vehicle.pojo.entity.User;
import cn.tedu._07vehicle.pojo.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/v1/uer/")
@Tag(name="用户管理模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("reg")
    @Operation(summary = "注册功能")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam) {
        log.debug("注册功能参数", userRegParam);
        /**
         * 1.接收客户端传递的数据[已完成];
         * 2.数据合法性的校验[后期使用验证框架springvalidation完成];
         * 3.确认用户名是否被占用[SELECT接口];
         * SELECT ... FROM user WHERE username=#{username}
         *  3.1被占用,则直接返回结束;
         *  3.2未被占用,则插入数据[INSERT],结束;
         */
        String username = userRegParam.getUsername();
        int count = userMapper.selectUserByUsername(username);
        if (count > 0) {
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    @PostMapping("login")
    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session) {
        log.debug("登录功能参数", userLoginParam);
        UserVo userVo = userMapper.selectUserByUsernameAndPassword(userLoginParam);
        if (userVo!=null) {
            //设置会话的标识到服务器的内存中;
            session.setAttribute("user", userVo);
            return JsonResult.ok();
        }else{ return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
    }
  }
  @GetMapping("currentUser")
  @Operation(summary = "获取当前用户")
  @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session) {
      UserVo userVo=(UserVo)session.getAttribute("user");
      /**
       * Web前端工程师:
       * if(response.data.userVO==null){
       *     //重定向到登录页面;
       *     response.sendR
       * }
       */
      return JsonResult.ok(userVo);
  }
  //4.退出登录功能
  @GetMapping("logout")
  @Operation(summary = "退出登录")
  @ApiOperationSupport(order = 40)
    public void logout(HttpSession session) {
        session.removeAttribute("user");

  }
}
